﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;

namespace TestResults
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.
    public class Service1 : ITestResultsService
    {
        /// <summary>
        /// Get test results for test_id that user_id took
        /// </summary>
        /// <param name="test_id">Encrypted test id</param>
        /// <param name="user_id">Encrypted user id</param>
        /// <returns></returns>
        public Test getTestResults(String test_id, String user_id)
        {
            Test test = new Test();

            try
            {
                EncryptionService.ServiceSoapClient encrypt_client =
                    new EncryptionService.ServiceSoapClient("ServiceSoap");

                // decrypt the test and user id
                String decrypted_test_id = encrypt_client.Decrypt(test_id);
                String decrypted_user_id = encrypt_client.Decrypt(user_id);

                // JSC TODO: Fetch the test here from the application server/database
                test = this._fetchSampleTest(decrypted_test_id, decrypted_user_id);
            }
            catch (Exception ex)
            {
                String message =
                    "Exception occured in getTestResults. " + ex.Message;
                Console.WriteLine(ex.Message);
            }

            return test;
        }

        /// <summary>
        /// Fet a sample test
        /// </summary>
        /// <param name="test_id"></param>
        /// <param name="user_id"></param>
        /// <returns></returns>
        private Test _fetchSampleTest(String test_id, String user_id)
        {
            Test test = new Test();

            //validate the user id with the test id to make sure it exists
            if (this._validateQuery(test_id, user_id))
            {
                test = _fetchTest(test_id, user_id);
            }
            return test;
        }

        /// <summary>
        /// Verify that the user_id and corresponding test_id exist in the data
        /// </summary>
        /// <param name="test_id">The requested test id</param>
        /// <param name="user_id">The corresponding user id</param>
        /// <returns></returns>
        private bool _validateQuery(String test_id, String user_id)
        {
            // JSC TODO: Modify this to use a database for a later assignment
            if (test_id == "1" && user_id == "1")
            {
                return true;
            }

            return false;
        }

        /// <summary>
        /// Retrieve the Test (test_id) for the given user (user_id)
        /// </summary>
        /// <param name="test_id">The requested test id</param>
        /// <param name="user_id">The corresponding user id</param>
        /// <returns></returns>
        private Test _fetchTest( String test_id, String user_id)
        {
            // JSC TODO: Change this to fetch from the database in a future assignment
            Test test = SampleData.get_sample();

            return test;
        }
    }
}
